package com.study.common.config;

import com.study.common.utils.DateUtil;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 全局类型转换器
 **/
public class ConvertFactory {

    @Component
    public static class StrToLocalDate implements Converter<String, LocalDate> {
        @Override
        public LocalDate convert(@NonNull String string) {
            return LocalDate.parse(string);
        }
    }

    @Component
    public static class StrToLocalDateTime implements Converter<String, LocalDateTime> {
        @Override
        public LocalDateTime convert(@NonNull String string) {
            return DateUtil.parseLocalDateTime(string);
        }
    }

    @Component
    public static class StrToLocalTime implements Converter<String, LocalTime> {
        @Override
        public LocalTime convert(@NonNull String string) {
            return DateUtil.parseLocalDateTime(string).toLocalTime();
        }
    }

    @Component
    public static class StrToInstant implements Converter<String, Instant> {
        @Override
        public Instant convert(@NonNull String string) {
            return DateUtil.parseInstant(string);
        }
    }

}